package reverseLinkedListIi;


//https://leetcode.cn/problems/reverse-linked-list-ii/
public class Solution {
    public ListNode reverseBetween(ListNode head, int left, int right) {
        ListNode dummy = new ListNode(0, head);
        ListNode p0 = dummy;
        int move = 1;
        while (move++ < left) {
            p0 = p0.next;
        }
        ListNode pre = null;
        ListNode cur = p0.next;
        for (int i = 0; i < (right - left + 1); i++) {
            ListNode ext = cur.next;
            cur.next = pre;
            pre = cur;
            cur = ext;
        }
        p0.next.next = cur;
        p0.next = pre;
        return dummy.next;
    }
}
